<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>

    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="faketabControl"
      class="fake-tab-control"
      v-show="isTabFixed"
    />
    <!-- 这里的@pullingUp="loadMore"是监听子组件发射出来的事件this.$emit('pullingUp') -->
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @FbackTopShow="scroll"
      :pullUpLoad="true"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" @swiperImgLoad.once="swiperImgLoad" />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl"
      />
      <!-- <tab-control
        class="tab-control"
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl"
        :class="{ fixed: isTabFixed }"
      /> -->
      <goods-list :goods="goods[currentType].list" />
    </scroll>

    <back-top @click.native="backClick" v-show="showBackTop"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
// import Scroll from "components/common/scroll/Scroll";
import Scroll from "../../components/common/scroll/Scroll.vue";

import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList"; //商品列表
import BackTop from "components/content/backTop/BackTop"; //回到顶部小按钮

//引入网络请求模块
import { getHomeMultidata, getHomeGoods } from "network/home";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      showBackTop: false,
      tabOffetTop: 0, //做tab的吸顶效果
      isTabFixed: false, //做tab的吸顶效果
    };
  },
  //生命周期钩子---创建组件时
  created() {
    // 1.请求多个数据
    // getHomeMultidata().then(res => {
    //   // this.result = res;
    //   this.banners = res.data.banner.list;
    //   this.recommends = res.data.recommend.list;
    // })
    this.getHomeMultidata();

    // getHomeGoods("pop", 1).then(res => {
    //   console.log(res);
    // });
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  mounted() {
    //事件总线
      // 1.图片加载完成的事件监听
    this.$bus.$on('itemImgLoad', () => {
      this.$refs.scroll.scroll.refresh();
      // console.log('事件总线');
    })
    // const refresh = this.$refs.scroll.scroll.refresh
    // this.$bus.$on('itemImageLoad', () => {
    //   refresh()
    // })

    //防抖后的事件总线
    // const refresh = this.debounce(this.$refs.scroll.scroll.refresh);
    // this.$bus.$on("itemImgLoad", () => {
    //   refresh();
    // });

    //获取tabOffetTop:0 //做tab的吸顶效果
    // 如果直接在mounted中获取tabControl的offsetTop, 那么值是不正确
    // console.log(this.$refs.tabControl.$el.offsetTop);
  },

  activated() {
    this.$refs.scroll.scroll.refresh()
  },
  methods: {
    /**
     * 防抖处理---未实现
     */
    debounce(func, delay) {
      let timer = null; //下面闭包对timer有引用，所以不会销毁
      return function (...args) {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
          func.apply(this, args);
        }, delay);
      };
    },
    /**
     * 图片加载
     */
    swiperImgLoad() {
      // console.log(this.$refs.tabControl.$el.offsetTop);
      this.tabOffetTop = this.$refs.tabControl.$el.offsetTop;
    },

    /**
     * 网络请求相关方法
     */

    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // this.result = res;
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        //调用其方法，使得可以多次下来加载更多
        this.$refs.scroll.scroll.finishPullUp();
      });
    },

    /**
    
    事件监听相关
     */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }

      //使两个tabControl的选项保持一致
      this.$refs.faketabControl.currentIndex = index
      this.$refs.tabControl.currentIndex = index
    },
    //控制返回顶部按钮的显示隐藏
    backClick() {
      this.$refs.scroll.scroll.scrollTo(0, 0, 500);
    },
    scroll(scrollData) {
      // console.log(scrollData)
      scrollData.y < -700
        ? (this.showBackTop = true)
        : (this.showBackTop = false);

      //判断是否吸顶
      this.isTabFixed = -scrollData.y > this.tabOffetTop;
    },
    //上拉加载更多
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
  },
};
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;

  /* 在使用原生滚动时，为了让导航不跟随一起滚动 */
  /* position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9; */
}

/* .tab-control {
  position: sticky;
  top: 44px;
} */

/* .content { */
/* height: calc(100vh - 93px); */
/* background-color: lawngreen; */
/* overflow: hidden; */
/* } */
.fake-tab-control {
  position: relative;
  z-index: 99;
}

.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

/* .fixed {
  position: fixed;
  left: 0;
  right: 0;
  top: 44px;
} */
</style>
